﻿using System;
using System.Windows.Forms;
using Infragistics.Win.UltraWinGrid;
using POS.BusinessComponents;
using POS.BusinessEntities;

namespace POS.UI.Mantenimientos
{
    public partial class MantContactosForm : MantForm
    {
        #region Declarations
        
        private TipoContacto tipoContacto;
        private int referencia;
        private int selectedId;
        private const string idColumn = "id";
        private const string nameColumn = "nombre";
       
        private readonly BCContacto bcEntidad = new BCContacto();

        #endregion

        #region Constructors

        public MantContactosForm(TipoContacto tipoContacto, int referencia)
        {
            InitializeComponent();
            this.tipoContacto = tipoContacto;
            this.referencia = referencia;
        }

        #endregion

        #region Event Handlers

        private void MantContactosForm_Load(object sender, EventArgs e)
        {
            LoadList();
            toolStrip.ItemClicked += toolStrip_ItemClicked;
        }

        private void toolStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Text)
            {
                case NewText:
                    New();
                    break;
            }
        }

        private void grid_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            ConfigureGrid((UltraGrid)sender);
        }

        private void grid_AfterRowActivate(object sender, EventArgs e)
        {
            selectedId = (int)grid.ActiveRow.Cells[idColumn].Value;
            UnPack((BEContacto)grid.ActiveRow.ListObject);
        }

        private void grid_AfterSelectChange(object sender, AfterSelectChangeEventArgs e)
        {
            EnableDisable();
        }

        private void grid_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Delete && grid.Selected.Rows.Count > 0)
                Delete();
        }


        #endregion

        #region Methods: Non-Public

        protected new static void ConfigureGrid(UltraGridBase listGrid)
        {
            MantForm.ConfigureGrid(listGrid);

            foreach (var col in listGrid.DisplayLayout.Bands[0].Columns)
                col.Hidden = true;

            listGrid.DisplayLayout.Bands[0].Columns[nameColumn].Hidden = false;
        }

        private void LoadList()
        {
            grid.DataSource = bcEntidad.GetList(tipoContacto, referencia);
            ClearGridSelection();
        }

        private void ClearGridSelection()
        {
            grid.Selected.Rows.Clear();
            grid.ActiveRow = null;
            EnableDisable();
        }

        protected override bool ValidateInput()
        {
            var name = nombreTextBox.Text.Trim();
            if (string.IsNullOrEmpty(name))
            {
                ShowError("Debe ingresar el nombre");
                return false;
            }
            return true;
        }

        private void EnableDisable()
        {
            toolStrip.Items[DeleteText].Enabled = selectedId > 0;
        }

        protected override void Save()
        {
            if (ValidateInput())
            {
                bcEntidad.Save(Pack());
                New();
                LoadList();
            }
        }

        protected override void Delete()
        {
            if (ShowQuestion("¿Está seguro de querer eliminar el contacto seleccionado?"))
            {   
                new BCEntidad(TipoEntidad.Contacto).Delete(selectedId);
                LoadList();
                ClearControls(Controls);
                ClearGridSelection();
                grid.Select();
            }
        }

        private void New()
        {
            ClearControls(Controls);
            nombreTextBox.Focus();
            selectedId = 0;
            ClearGridSelection();
        }

        private BEContacto Pack()
        {
            var entity = new BEContacto
            {
                Id = selectedId,
                Nombre = nombreTextBox.Text.Trim(),
                Celular = celularTextBox.Text.Trim(),
                Email = emailTextBox.Text.Trim(),
                Notas = notasTextBox.Text.Trim(),
                Puesto = puestoTextBox.Text.Trim(),
                Telefono = telefonoTextBox.Text.Trim(),
                Tipo = tipoContacto,
                Referencia = referencia
            };
            return entity;
        }

        private void UnPack(BEContacto entity)
        {
            nombreTextBox.Text = entity.Nombre;
            puestoTextBox.Text = entity.Puesto;
            emailTextBox.Text = entity.Email;
            telefonoTextBox.Text = entity.Telefono;
            celularTextBox.Text = entity.Celular;
            notasTextBox.Text = entity.Notas;
        }

        #endregion
        
    }
}
